package org.rambadger.command.resource;


import com.fasterxml.jackson.annotation.JsonProperty;
import com.sun.jersey.api.NotFoundException;
import com.yammer.dropwizard.auth.Auth;
import org.mongojack.DBCursor;
import org.mongojack.DBQuery;
import org.mongojack.JacksonDBCollection;
import org.rambadger.command.auth.AuthUser;
import org.rambadger.command.axon.JCollection;
import org.hibernate.validator.constraints.NotEmpty;
import org.rambadger.command.representation.Platform;
import org.rambadger.command.representation.Track;
import org.rambadger.command.view.PlatformView;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Path("/platform/")
public class ViewPlatformResource {
    @JCollection(type=Platform.class)
    private JacksonDBCollection<Platform, String> platforms = null;

    @JCollection(type=Track.class)
    private JacksonDBCollection<Track, String> tracks = null;




    @GET @Path("{platformName}")
    @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON})
    public Object getPlatformList(@Auth(required = true) AuthUser user, @PathParam("platformName") @NotEmpty String platformName) {
        Platform platform = platforms.findOneById(platformName);
        if (platform == null) {
            throw new NotFoundException();
        } else {
            DBCursor<Track> found = tracks.find(DBQuery.is("_id.platformId", platform.getPlatformId()));
            ArrayList<Track> trackInfo = new ArrayList<Track>();
            int i = 0;
            while (found.hasNext()){
                Track current = found.next();
                trackInfo.add(current);
                i++;
            }
            return new PlatformView(user, platform, trackInfo);
        }
    }

}

